package com.example.demo.controller;

import com.example.demo.entity.ApiResponse;
import com.example.demo.entity.Orders;
import com.example.demo.service.OrdersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OrderController {
    private final static Logger loger = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    private OrdersService ordersService;
    //生成订单接口，接收购物车中的图书信息并生成订单。
    @PreAuthorize("hasAuthority('order:create')")
    @PostMapping("/orders")
    public ResponseEntity<ApiResponse> addOrders(@RequestBody Orders orders){
        ApiResponse apiResponse;
        try {
            int line = ordersService.createOrder(orders);
            if (line>0){
                apiResponse = new ApiResponse(1,"订单生成成功", null);
            }
            else {
                apiResponse = new ApiResponse(0,"订单生成失败", null);
            }
            loger.info("数据：{}",orders);
            return ResponseEntity.ok(apiResponse);
        }
        catch (Exception e){
            apiResponse = new ApiResponse(2,"订单生成失败", null);
            loger.info("数据：{}",orders);
            loger.error("订单生成失败：{}",e.getMessage());
            return ResponseEntity.ok(apiResponse);
        }
    }

    //取所有订单接口，返回用户所有订单列表。
    @PreAuthorize("hasAuthority('order:read')")
    @GetMapping("/orders/{user_id}")
    public ResponseEntity<ApiResponse> getList(@PathVariable("user_id") int userId){
        ApiResponse apiResponse;
        List<Orders> list;
        try {
            list = ordersService.getList(userId);
            if (list.size() != 0) {
                apiResponse = new ApiResponse(1, "订单列表获取成功", list);
            }else {
                apiResponse = new ApiResponse(0, "订单列表为空", null);
            }
            loger.info("用户ID：{}",userId);
            return ResponseEntity.ok(apiResponse);
        }
        catch (Exception e){
            apiResponse = new ApiResponse(2,"订单列表获取失败", null);
            loger.info("用户ID：{}",userId);
            loger.error("订单列表获取失败：{}",e.getMessage());
            return ResponseEntity.ok(apiResponse);
        }
    }
    //获取单个订单详情接口，根据订单ID返回订单详细信息
    @PreAuthorize("hasAuthority('order:read')")
    @GetMapping("/orders/details/{order_id}")
    public ResponseEntity<ApiResponse> getOrder(@PathVariable("order_id") int orderId){
        ApiResponse apiResponse;
        Orders orders;
        try {
            orders = ordersService.getOrder(orderId);
            if (orders != null) {
                apiResponse = new ApiResponse(1, "订单详情获取成功", orders);
            }else {
                apiResponse = new ApiResponse(0, "订单详情为空", null);
            }
            loger.info("订单ID：{}",orderId);
            return ResponseEntity.ok(apiResponse);
        }
        catch (Exception e){
            apiResponse = new ApiResponse(2,"订单详情获取失败", null);
            loger.info("订单ID：{}",orderId);
            loger.error("订单详情获取失败：{}",e.getMessage());
            return ResponseEntity.ok(apiResponse);
        }
    }
    //更新订单状态接口，根据订单ID更新订单状态。
    @PreAuthorize("hasAuthority('order:update')")
    @PutMapping("/orders/{order_id}")
    public ResponseEntity<ApiResponse> updateOrderStatus(@PathVariable("order_id") int orderId, @RequestParam("status") String status){
        ApiResponse apiResponse;
        try {
            int line = ordersService.updateOrderStatus(orderId, status);
            if (line>0){
                apiResponse = new ApiResponse(1,"订单状态更新成功", null);
            }
            else {
                apiResponse = new ApiResponse(0,"订单状态更新失败", null);
            }
            loger.info("订单ID：{}",orderId);
            return ResponseEntity.ok(apiResponse);
        }
        catch (Exception e){
            apiResponse = new ApiResponse(2,"订单状态更新失败", null);
            loger.info("订单ID：{}",orderId);
            loger.error("订单状态更新失败：{}",e.getMessage());
            return ResponseEntity.ok(apiResponse);
        }
    }
}
